package org.swift.mashup.engine.concurrency.execution;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.swift.mashup.engine.concurrency.agenda.ActionTemplate;


public class Rule {
	private String name;
	private List<Condition> conditions = new ArrayList<Condition>();
	private Map<String, Parameter> parameters = new HashMap<String, Parameter>();
	private List<ActionTemplate> actions = new ArrayList<ActionTemplate>();
	private RulePattern pattern = null;	
	private boolean publishResults = false;

	public Rule(){}
	public Rule(String name){
		this.setName(name);	
	}
	public Rule(String name, boolean publish){
		this.setName(name);	
		this.setPublishResults(publish);
	}
	public RulePattern getPattern() {
		return pattern;
	}
	public void setPattern(RulePattern pattern) {
		this.pattern = pattern;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Condition> getConditions() {
		return conditions;
	}
	public void addCondition(String expression){
		addCondition(expression, null);
	}
	public void addCondition(String expression, Map<String,String> namespaces){
		Condition condition = new Condition(expression, namespaces);
		this.getConditions().add(condition);
	}
	
	/*
	 * (non-javadoc)
	 */
	private RuleSet ruleSet = null;
	 
	/**
	 * Getter of the property <tt>ruleSet</tt>
	 *
	 * @return Returns the ruleSet.
	 * 
	 */
	public RuleSet getRuleSet()
	{
		return ruleSet;
	}
	
	/**
	 * Setter of the property <tt>ruleSet</tt>
	 *
	 * @param ruleSet The ruleSet to set.
	 *
	 */
	public void setRuleSet(RuleSet ruleSet ){
		this.ruleSet = ruleSet;
	}

		
	
	/**
	 * Getter of the property <tt>parameter</tt>
	 *
	 * @return Returns the parameter.
	 * 
	 */
	public Map<String, Parameter> getParameters()
	{
		return parameters;
	}
	 
	public void addParameter(String name, String value){
		Parameter param = new Parameter(name,value,this);
		this.getParameters().put(value, param);
	}
	
	/**
	 * Setter of the property <tt>parameter</tt>
	 *
	 * @param parameter the parameter to set.
	 *
	 */
	public void setParameters(Map<String, ? extends Parameter> parameters){
		this.parameters.clear();
		this.parameters.putAll(parameters);
	}
	
	/**
	 * Removes a single instance of the specified element from this
	 * collection, if it is present (optional operation).
	 *
	 * @param element to be removed from this collection, if present.
	 * @see	java.util.Collection#add(Object)
	 *
	 */
	public void removeParameter(String name){
		parameters.remove(name);
	}
	
	/**
	 * Removes all of the elements from this collection (optional operation).
	 *
	 * @see	java.util.Collection#clear()
	 *
	 */
	public void clearParameters(){
		parameters.clear();
	}
	public List<ActionTemplate> getActions() {
		return actions;
	}
	public void setActions(List<? extends ActionTemplate> actions) {
		this.actions.clear();
		this.actions.addAll(actions);
	}
	public boolean isPublishResults() {
		return publishResults;
	}
	public void setPublishResults(boolean publishResults) {
		this.publishResults = publishResults;
	}
	@Override
	public String toString() {
		return "Rule [" + this.getName() + "] - " + this.getConditions();
	}
	
	
}
